package com.visionet.waveletData.web.repository.group.impl;

import com.visionet.waveletData.web.vo.RobotVo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hxpeng on 2017/1/11.
 */
@Repository
public class RobotGroupDaoImpl {

    private final static Logger logger = LoggerFactory.getLogger(RobotGroupDaoImpl.class);

    @Resource
    private JdbcTemplate jdbcTemplate;

    public PageImpl<RobotVo> findPageRobotFromGroup(String groupId, Pageable page){
        StringBuffer sql = new StringBuffer();
        List<Object> params = new ArrayList<Object>();
        sql.append("select pr.id,pr.robot_number,pr.company_id,pr.create_by,pr.create_date,pr.update_by,pr.update_date,pr.is_deleted,prg.create_date as push_date ");
        sql.append("from p_robot as pr, p_robot_group as prg where pr.id = prg.robot_id and pr.is_deleted = '0' and prg.is_deleted = '0' and prg.group_id = ? order by push_date desc ");
        params.add(groupId);

        if(page == null){
            page = new PageRequest(0, 10);
        }
        int currentPage = page.getPageNumber();

        int pageSize = page.getPageSize();
        int size = jdbcTemplate.queryForInt("select count(*) from ("+sql.toString()+") as c", params.toArray());
        List<RobotVo> list = jdbcTemplate.query("select t.* from ("+sql+") as t limit  "+ currentPage * pageSize+","+pageSize, params.toArray(), new RowMapper<RobotVo>() {
            @Override
            public RobotVo mapRow(ResultSet rs, int rowNum) throws SQLException {
                RobotVo entity = new RobotVo();
                entity.setId(rs.getLong("id"));
                entity.setRobotNumber(rs.getString("robot_number"));
                entity.setCompanyId(rs.getString("company_id"));
                entity.setCreateBy(rs.getString("create_by"));
                entity.setCreateDate(rs.getTimestamp("create_date"));
                entity.setUpdateBy(rs.getString("update_by"));
                entity.setUpdateDate(rs.getTimestamp("update_date"));
                entity.setIsDeleted(rs.getInt("is_deleted"));
                entity.setPushInGroupTime(rs.getTimestamp("push_date"));
                return entity;
            }
        });
        PageImpl<RobotVo> cmProductGroupTourPage = new PageImpl<RobotVo>(list, page, size);
        return cmProductGroupTourPage;
    }


    public int findCountByGroupId(String groupId){
        StringBuffer sql = new StringBuffer();
        List<Object> params = new ArrayList<Object>();
        sql.append("select prg.id,prg.is_deleted from p_robot_group as prg where prg.is_deleted='0' ");
        if(StringUtils.isNotBlank(groupId)){
            sql.append(" and prg.group_id = ?");
            params.add(groupId);
        }
        int size = jdbcTemplate.queryForInt("select count(*) from ("+sql.toString()+") as c", params.toArray());
        return size;
    }

}
